package com.shuhe.handler;

import com.alibaba.fastjson.JSONObject;
import com.shuhe.domain.api.ApiRequestBody;
import com.shuhe.metastore.MetaMysql;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;
import java.util.UUID;

import static com.shuhe.common.CommonFunction.*;


public class PdfApiHandler extends HttpServlet {

    private static Logger logger = Logger.getLogger(PdfApiHandler.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {


            resp = setResponse(resp);
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("application/json");
            String tokenKey = req.getHeader("token");
            if (tokenKey == null) {
                return;
            }

            String resultStr = "";

            resp.getWriter().write(resultStr);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp = setResponse(resp);
        //文件上传
        if (ServletFileUpload.isMultipartContent(req)) {
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setHeaderEncoding("UTF-8");
            try {
                // 解析请求的内容提取文件数据
                List<FileItem> formItems = upload.parseRequest(req);

                if (formItems != null && formItems.size() > 0) {
                    // 迭代表单数据
                    for (FileItem item : formItems) {
                        // 处理不在表单中的字段
                        if (!item.isFormField()) {
                            String name = new File(item.getName()).getName();
                            String filePath = System.getProperty("user.dir");
                            filePath = filePath + File.separator + name;
                            logger.info("filePath:" + filePath);
                            File storeFile = new File(filePath);
                            item.write(storeFile);
                        }
                    }
                }
                resp.getWriter().write("文件上传成功!");
                return;
            } catch (Exception ex) {
                ex.printStackTrace();
                resp.getWriter().write("文件上传失败!");
                return;
            }
        }
        //非文件上传
        Connection metaConn = null;
        try {
            metaConn = MetaMysql.connect();
            resp = setResponse(resp);
            String bodyStr = readAsChars(req);
            JSONObject bodyJson = JSONObject.parseObject(bodyStr);
            ApiRequestBody apiReq = JSONObject.toJavaObject(bodyJson, ApiRequestBody.class);

            resp.getWriter().write("");

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            closeDb(metaConn);
        }
    }


    @Override
    protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp = setResponse(resp);
    }
}
